In [7]:
!pip install yfinance
!pip install bs4
!pip install nbformat
!pip install --upgrade plotly
!pip install lxml
!pip uninstall -y plotly
!pip install plotly==5.18.0
Requirement already satisfied: yfinance in c:\users\akkur\anaconda3\lib\site-packages (0.2.59)
Requirement already satisfied: pandas>=1.3.0 in c:\users\akkur\anaconda3\lib\site-packages (from yfinance) (2.2.2)
Requirement already satisfied: numpy>=1.16.5 in c:\users\akkur\anaconda3\lib\site-packages (from yfinance) (1.26.4)
Requirement already satisfied: requests>=2.31 in c:\users\akkur\anaconda3\lib\site-packages (from yfinance) (2.32.3)
Requirement already satisfied: multitasking>=0.0.7 in c:\users\akkur\anaconda3\lib\site-packages (from yfinance) (0.0.11)
Requirement already satisfied: platformdirs>=2.0.0 in c:\users\akkur\anaconda3\lib\site-packages (from yfinance) (3.10.0)
Requirement already satisfied: pytz>=2022.5 in c:\users\akkur\anaconda3\lib\site-packages (from yfinance) (2024.1)
Requirement already satisfied: frozendict>=2.3.4 in c:\users\akkur\anaconda3\lib\site-packages (from yfinance) (2.4.2)
Requirement already satisfied: peewee>=3.16.2 in c:\users\akkur\anaconda3\lib\site-packages (from yfinance) (3.18.1)
Requirement already satisfied: beautifulsoup4>=4.11.1 in c:\users\akkur\anaconda3\lib\site-packages (from yfinance) (4.12.3)
Requirement already satisfied: curl_cffi>=0.7 in c:\users\akkur\anaconda3\lib\site-packages (from yfinance) (0.10.0)
Requirement already satisfied: protobuf<6,>=5.29.0 in c:\users\akkur\anaconda3\lib\site-packages (from yfinance) (5.29.4)
Requirement already satisfied: websockets>=11.0 in c:\users\akkur\anaconda3\lib\site-packages (from yfinance) (15.0.1)
Requirement already satisfied: soupsieve>1.2 in c:\users\akkur\anaconda3\lib\site-packages (from beautifulsoup4>=4.11.1->yfinance) (2.5)
Requirement already satisfied: cffi>=1.12.0 in c:\users\akkur\anaconda3\lib\site-packages (from curl_cffi>=0.7->yfinance) (1.17.1)
Requirement already satisfied: certifi>=2024.2.2 in c:\users\akkur\anaconda3\lib\site-packages (from curl_cffi>=0.7->yfinance) (2024.12.14)
Requirement already satisfied: python-dateutil>=2.8.2 in c:\users\akkur\anaconda3\lib\site-packages (from pandas>=1.3.0->yfinance) (2.9.0.post0)
Requirement already satisfied: tzdata>=2022.7 in c:\users\akkur\anaconda3\lib\site-packages (from pandas>=1.3.0->yfinance) (2023.3)
Requirement already satisfied: charset-normalizer<4,>=2 in c:\users\akkur\anaconda3\lib\site-packages (from requests>=2.31->yfinance) (3.3.2)
Requirement already satisfied: idna<4,>=2.5 in c:\users\akkur\anaconda3\lib\site-packages (from requests>=2.31->yfinance) (3.7)
Requirement already satisfied: urllib3<3,>=1.21.1 in c:\users\akkur\anaconda3\lib\site-packages (from requests>=2.31->yfinance) (2.2.3)
Requirement already satisfied: pycparser in c:\users\akkur\anaconda3\lib\site-packages (from cffi>=1.12.0->curl_cffi>=0.7->yfinance) (2.21)
Requirement already satisfied: six>=1.5 in c:\users\akkur\anaconda3\lib\site-packages (from python-dateutil>=2.8.2->pandas>=1.3.0->yfinance) (1.16.0)
Collecting bs4
Downloading bs4-0.0.2-py2.py3-none-any.whl.metadata (411 bytes)
Requirement already satisfied: beautifulsoup4 in c:\users\akkur\anaconda3\lib\site-packages (from bs4) (4.12.3)
Requirement already satisfied: soupsieve>1.2 in c:\users\akkur\anaconda3\lib\site-packages (from beautifulsoup4->bs4) (2.5)
Downloading bs4-0.0.2-py2.py3-none-any.whl (1.2 kB)
Installing collected packages: bs4
Successfully installed bs4-0.0.2
Requirement already satisfied: nbformat in c:\users\akkur\anaconda3\lib\site-packages (5.10.4)
Requirement already satisfied: fastjsonschema>=2.15 in c:\users\akkur\anaconda3\lib\site-packages (from nbformat) (2.16.2)
Requirement already satisfied: jsonschema>=2.6 in c:\users\akkur\anaconda3\lib\site-packages (from nbformat) (4.23.0)
Requirement already satisfied: jupyter-core!=5.0.*,>=4.12 in c:\users\akkur\anaconda3\lib\site-packages (from nbformat) (5.7.2)
Requirement already satisfied: traitlets>=5.1 in c:\users\akkur\anaconda3\lib\site-packages (from nbformat) (5.14.3)
Requirement already satisfied: attrs>=22.2.0 in c:\users\akkur\anaconda3\lib\site-packages (from jsonschema>=2.6->nbformat) (23.1.0)
Requirement already satisfied: jsonschema-specifications>=2023.03.6 in c:\users\akkur\anaconda3\lib\site-packages (from jsonschema>=2.6->nbformat) (2023.7.1)
Requirement already satisfied: referencing>=0.28.4 in c:\users\akkur\anaconda3\lib\site-packages (from jsonschema>=2.6->nbformat) (0.30.2)
Requirement already satisfied: rpds-py>=0.7.1 in c:\users\akkur\anaconda3\lib\site-packages (from jsonschema>=2.6->nbformat) (0.10.6)
Requirement already satisfied: platformdirs>=2.5 in c:\users\akkur\anaconda3\lib\site-packages (from jupyter-core!=5.0.*,>=4.12->nbformat) (3.10.0)
Requirement already satisfied: pywin32>=300 in c:\users\akkur\anaconda3\lib\site-packages (from jupyter-core!=5.0.*,>=4.12->nbformat) (305.1)
Requirement already satisfied: plotly in c:\users\akkur\anaconda3\lib\site-packages (5.24.1)
Collecting plotly
Downloading plotly-6.0.1-py3-none-any.whl.metadata (6.7 kB)
Collecting narwhals>=1.15.1 (from plotly)
Downloading narwhals-1.38.2-py3-none-any.whl.metadata (9.4 kB)
Requirement already satisfied: packaging in c:\users\akkur\anaconda3\lib\site-packages (from plotly) (24.1)
Downloading plotly-6.0.1-py3-none-any.whl (14.8 MB)
---------------------------------------- 0.0/14.8 MB ? eta -:--:--
------------------------------- -------- 11.5/14.8 MB 65.5 MB/s eta 0:00:01
---------------------------------------- 14.8/14.8 MB 62.2 MB/s eta 0:00:00
Downloading narwhals-1.38.2-py3-none-any.whl (338 kB)
Installing collected packages: narwhals, plotly
Attempting uninstall: plotly
Found existing installation: plotly 5.24.1
Uninstalling plotly-5.24.1:
Successfully uninstalled plotly-5.24.1
Successfully installed narwhals-1.38.2 plotly-6.0.1
Requirement already satisfied: lxml in c:\users\akkur\anaconda3\lib\site-packages (5.2.1)
Found existing installation: plotly 6.0.1
Uninstalling plotly-6.0.1:
Successfully uninstalled plotly-6.0.1
Collecting plotly==5.18.0
Downloading plotly-5.18.0-py3-none-any.whl.metadata (7.0 kB)
Requirement already satisfied: tenacity>=6.2.0 in c:\users\akkur\anaconda3\lib\site-packages (from plotly==5.18.0) (8.2.3)
Requirement already satisfied: packaging in c:\users\akkur\anaconda3\lib\site-packages (from plotly==5.18.0) (24.1)
Downloading plotly-5.18.0-py3-none-any.whl (15.6 MB)
---------------------------------------- 0.0/15.6 MB ? eta -:--:--
------------------------- -------------- 10.0/15.6 MB 56.4 MB/s eta 0:00:01
---------------------------------------- 15.6/15.6 MB 54.5 MB/s eta 0:00:00
Installing collected packages: plotly
Successfully installed plotly-5.18.0
In [17]:
import yfinance as yf
import pandas as pd
import requests
from bs4 import BeautifulSoup
import plotly.graph_objects as go
from plotly.subplots import make_subplots
In [19]:
import plotly.io as pio
pio.renderers.default = "iframe"
In [10]:
import warnings
# Ignore all warnings
warnings.filterwarnings("ignore", category=FutureWarning)
In [21]:
def make_graph(stock_data, revenue_data, stock):
fig = make_subplots(rows=2, cols=1, shared_xaxes=True, subplot_titles=("Historical Share Price", "Historical Revenue"), vertical_spacing = .3)
stock_data_specific = stock_data[stock_data.Date <= '2021-06-14']
revenue_data_specific = revenue_data[revenue_data.Date <= '2021-04-30']
fig.add_trace(go.Scatter(x=pd.to_datetime(stock_data_specific.Date, infer_datetime_format=True), y=stock_data_specific.Close.astype("float"), name="Share Price"), row=1, col=1)
fig.add_trace(go.Scatter(x=pd.to_datetime(revenue_data_specific.Date, infer_datetime_format=True), y=revenue_data_specific.Revenue.astype("float"), name="Revenue"), row=2, col=1)
fig.update_xaxes(title_text="Date", row=1, col=1)
fig.update_xaxes(title_text="Date", row=2, col=1)
fig.update_yaxes(title_text="Price ($US)", row=1, col=1)
fig.update_yaxes(title_text="Revenue ($US Millions)", row=2, col=1)
fig.update_layout(showlegend=False,
height=900,
title=stock,
xaxis_rangeslider_visible=True)
fig.show()
from IPython.display import display, HTML
fig_html = fig.to_html()
display(HTML(fig_html))
Question 1: Use yfinance to Extract Stock Data
In [23]:
tesla = yf.Ticker("TSLA")
In [25]:
tesla_data = tesla.history(period="max")
In [27]:
tesla_data.reset_index(inplace=True)
tesla_data.head()
Out[27]:
| Date | Open | High | Low | Close | Volume | Dividends | Stock Splits | |
|---|---|---|---|---|---|---|---|---|
| 0 | 2010-06-29 00:00:00-04:00 | 1.266667 | 1.666667 | 1.169333 | 1.592667 | 281494500 | 0.0 | 0.0 |
| 1 | 2010-06-30 00:00:00-04:00 | 1.719333 | 2.028000 | 1.553333 | 1.588667 | 257806500 | 0.0 | 0.0 |
| 2 | 2010-07-01 00:00:00-04:00 | 1.666667 | 1.728000 | 1.351333 | 1.464000 | 123282000 | 0.0 | 0.0 |
| 3 | 2010-07-02 00:00:00-04:00 | 1.533333 | 1.540000 | 1.247333 | 1.280000 | 77097000 | 0.0 | 0.0 |
| 4 | 2010-07-06 00:00:00-04:00 | 1.333333 | 1.333333 | 1.055333 | 1.074000 | 103003500 | 0.0 | 0.0 |
Question 2: Use Webscraping to Extract Tesla Revenue Data
In [29]:
import pandas as pd
import requests
from bs4 import BeautifulSoup
In [31]:
url = "https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0220EN-SkillsNetwork/labs/project/revenue.htm"
html_data = requests.get(url).text
In [33]:
soup = BeautifulSoup(html_data, "html.parser")
tesla_revenue = pd.DataFrame(columns=["Date", "Revenue"])
revenue_table = soup.find_all("tbody")[1]
rows_list = []
for row in revenue_table.find_all("tr"):
cols = row.find_all("td")
if len(cols) == 2:
date = cols[0].text.strip()
revenue = cols[1].text.strip()
rows_list.append({"Date": date, "Revenue": revenue})
tesla_revenue = pd.DataFrame(rows_list)
In [35]:
tesla_revenue["Revenue"] = tesla_revenue["Revenue"].str.replace("$", "").str.replace(",", "")
In [37]:
tesla_revenue["Revenue"].replace("", pd.NA, inplace=True)
tesla_revenue.dropna(inplace=True)
In [39]:
tesla_revenue.tail()
Out[39]:
| Date | Revenue | |
|---|---|---|
| 48 | 2010-09-30 | 31 |
| 49 | 2010-06-30 | 28 |
| 50 | 2010-03-31 | 21 |
| 52 | 2009-09-30 | 46 |
| 53 | 2009-06-30 | 27 |
Question 3: Use yfinance to Extract Stock Data
In [41]:
import yfinance as yf
import pandas as pd
gme = yf.Ticker("GME")
In [43]:
gme_data = gme.history(period="max")
In [45]:
gme_data.reset_index(inplace=True)
gme_data.head()
Out[45]:
| Date | Open | High | Low | Close | Volume | Dividends | Stock Splits | |
|---|---|---|---|---|---|---|---|---|
| 0 | 2002-02-13 00:00:00-05:00 | 1.620128 | 1.693350 | 1.603296 | 1.691667 | 76216000 | 0.0 | 0.0 |
| 1 | 2002-02-14 00:00:00-05:00 | 1.712707 | 1.716074 | 1.670626 | 1.683250 | 11021600 | 0.0 | 0.0 |
| 2 | 2002-02-15 00:00:00-05:00 | 1.683250 | 1.687458 | 1.658002 | 1.674834 | 8389600 | 0.0 | 0.0 |
| 3 | 2002-02-19 00:00:00-05:00 | 1.666418 | 1.666418 | 1.578047 | 1.607504 | 7410400 | 0.0 | 0.0 |
| 4 | 2002-02-20 00:00:00-05:00 | 1.615920 | 1.662210 | 1.603296 | 1.662210 | 6892800 | 0.0 | 0.0 |
Question 4: Use Webscraping to Extract GME Revenue Data
In [47]:
url = "https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0220EN-SkillsNetwork/labs/project/stock.html"
html_data_2 = requests.get(url).text
In [49]:
soup = BeautifulSoup(html_data_2, "html.parser")
In [51]:
gme_revenue = pd.DataFrame(columns=["Date", "Revenue"])
revenue_table = soup.find_all("tbody")[1]
rows = []
for row in revenue_table.find_all("tr"):
cols = row.find_all("td")
if len(cols) == 2:
date = cols[0].text.strip()
revenue = cols[1].text.strip()
rows.append({"Date": date, "Revenue": revenue})
gme_revenue = pd.DataFrame(rows)
gme_revenue["Revenue"] = gme_revenue["Revenue"].str.replace("$", "", regex=False).str.replace(",", "", regex=False)
In [53]:
gme_revenue.dropna(inplace=True)
gme_revenue.tail()
Out[53]:
| Date | Revenue | |
|---|---|---|
| 57 | 2006-01-31 | 1667 |
| 58 | 2005-10-31 | 534 |
| 59 | 2005-07-31 | 416 |
| 60 | 2005-04-30 | 475 |
| 61 | 2005-01-31 | 709 |
Question 5: Plot Tesla Stock Graph
In [55]:
make_graph(tesla_data, tesla_revenue, 'Tesla')
C:\Users\akkur\AppData\Local\Temp\ipykernel_22696\109047474.py:5: UserWarning: The argument 'infer_datetime_format' is deprecated and will be removed in a future version. A strict version of it is now the default, see https://pandas.pydata.org/pdeps/0004-consistent-to-datetime-parsing.html. You can safely remove this argument. C:\Users\akkur\AppData\Local\Temp\ipykernel_22696\109047474.py:6: UserWarning: The argument 'infer_datetime_format' is deprecated and will be removed in a future version. A strict version of it is now the default, see https://pandas.pydata.org/pdeps/0004-consistent-to-datetime-parsing.html. You can safely remove this argument.
Question 6: Plot GameStop Stock Graph
In [57]:
make_graph(gme_data, gme_revenue, 'GameStop')
C:\Users\akkur\AppData\Local\Temp\ipykernel_22696\109047474.py:5: UserWarning: The argument 'infer_datetime_format' is deprecated and will be removed in a future version. A strict version of it is now the default, see https://pandas.pydata.org/pdeps/0004-consistent-to-datetime-parsing.html. You can safely remove this argument. C:\Users\akkur\AppData\Local\Temp\ipykernel_22696\109047474.py:6: UserWarning: The argument 'infer_datetime_format' is deprecated and will be removed in a future version. A strict version of it is now the default, see https://pandas.pydata.org/pdeps/0004-consistent-to-datetime-parsing.html. You can safely remove this argument.
In [ ]: